﻿using MMS.Api.Helper;
using MMS.Model.FormatModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.WebPages;

namespace MMS.Api.Areas.Admin.Controllers
{
    public class GoodsInfoController : BaseController
    {
        #region 1.0 分页查询  //http://localhost:16824/api/GoodsInfo/GetWherePaged
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">页码</param>
        /// <param name="id1">页容量</param>
        /// <param name="id2">排序条件</param>
        /// <param name="id3">升序或者降序</param>
        /// <returns></returns>
        public PageData<Model.GoodsInfo> GetWherePaged(string id, string id1, string id2, string id3)
        {
            int pageIndex = id.AsInt();
            int pageSize = id1.AsInt();


            bool isAsc = true;
            if (id3 == "asc")
                isAsc = true;
            else
                isAsc = false;

            //1.查询数据集合
            var pageData = OpeCur.BLLSession.GoodsInfo.WherePaged(pageIndex, pageSize, o => o.GIsDel == false, o => o.GInventory, isAsc, "Category");
            pageData.rows = pageData.rows.Select(o => o.ToPOCO(true)).ToList();
            //2.转成json格式字符串

            return pageData;
        }
        #endregion

        #region 2.0 新增 //http://localhost:16824/api/GoodsInfo/PostAdd
        public void PostAdd(Model.GoodsInfo model)
        {
            OpeCur.BLLSession.GoodsInfo.Add(model);
            OpeCur.BLLSession.SaveChanges();
        }
        #endregion

        #region 3.0 查找修改 //http://localhost:16824/api/GoodsInfo/GetModify/1
        public Model.GoodsInfo GetModify(int id)
        {
            var model = OpeCur.BLLSession.GoodsInfo.Where(o => o.GId == id && o.GIsDel == false).FirstOrDefault().ToPOCO();
            return model;
        }
        #endregion

        #region 3.1 修改 //http://localhost:16824/api/GoodsInfo/PostModify
        public void PostModify(Model.GoodsInfo model)
        {
            OpeCur.BLLSession.GoodsInfo.Modify(model, "GName", "GCId", "GCostPrice", "GSellPrice", "GInventory", "GInmin", "GInmax", "GIntegralRatio", "GRemark");
            OpeCur.BLLSession.SaveChanges();
        }
        #endregion

        #region 4.0 删除 // http://localhost:16824/api/GoodsInfo/GetRemoveById/
        public void GetRemoveById(int id)
        {
            OpeCur.BLLSession.GoodsInfo.ModifyBy(o => o.GId == id, new string[] { "GIsDel" }, new object[] { true });
            OpeCur.BLLSession.SaveChanges();
        }
        #endregion

        #region 5.0 查询  //http://localhost:16824/api/GoodsInfo/GetAllCategory
        public List<Model.Category> GetAllCategory()
        {
            var list = OpeCur.BLLSession.Category.Where(o => o.CIsDel == false).ToList().Select(o => o.ToPOCO()).ToList(); ;
            return list;
        }
        #endregion

        #region 6.0 销售分析  //http://localhost:16824/api/GoodsInfo/GetTrade
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="id1">datepart</param>
        /// <param name="id2">startdate</param>
        /// <param name="id3">enddate</param>
        /// <returns></returns>
        public List<class_Trend> GetTrend(int id, string id1, string id2, string id3)
        {
            if (id2.Length == 14)
            {
                DateTime startdate = DateTime.ParseExact(id2, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
                id2 = startdate.ToString();
                DateTime enddate = DateTime.ParseExact(id3, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
                id3=enddate.ToString();
            }
            string sql = "use MMS;with t as(select s.SITime as SaleTime,COUNT(*) as SalesVolume,SUM(s.GCostPrice) as CostPrice,SUM(s.SIActualPay)-SUM(s.GCostPrice) as profit	from(select*from SaleItem as SI	left join GoodsInfo as G on SI.SIGId=G.GId where SI.SITime between '" + id2 + "' and '" + id3 + "' and SI.SIGId=" + id + ") as s	group by s.SITime)";


            sql += " select SUM(SalesVolume) as SalesVolume,SUM(CostPrice) as CostPrice,SUM(profit) as profit,SaleTime from ( select 	SalesVolume	,CostPrice,profit,datepart(" + id1 + ",SaleTime) as SaleTime from t) as a group by  SaleTime";

            var list = OpeCur.BLLSession.GoodsInfo.SqlQuery<class_Trend>(sql).ToList();
            return list;
        }
        #endregion


    }
}
